<?php
namespace app\admin\controller;

use app\admin\model\WorkOrder as WorkOrderModel;
use app\admin\model\Account as AccountModel;
use app\admin\model\Device as DeviceModel;
use think\Db;
use think\Request;

class WorkOrder extends Base
{
    public function index()
    {
        $model = new WorkOrderModel;
        $aModel = new AccountModel;
        $dModel = new DeviceModel;

        //渲染页面模板
        $model->field('o.id,o.title,o.detail,o.status,o.remark,o.created_at,o.updated_at,u.username, d.name as dname, a.username as adname')
            ->alias('o')
            ->join('user u','o.user_id = u.id ','left')
            ->join('device d','o.device_id = d.id','left')
            ->join('admin a','o.remark_admin_id = a.id','left')
            ->order('o.id','desc');
        //->select();
        // ->paginate(5);

        //搜索名称
        $phname = $this->request->param("phname");

        if(!empty($phname)){
            $model->where('o.title','like', '%'.$phname.'%');
        }
        $result = $model->paginate(10,false,[
            'query'=>['phname'=>$phname]
        ]);

        //渲染用户组
        $account = $aModel->field('id,username')->select();
        //渲染代理商
        $device = $dModel->field('id,name')->select();

        //计算总共记录条数
        $count = $result->total();
        //分页
        $page = $result->render();

        //接收ajax传递的值
        $request = request();
        $ispost = $request->isPost();
        if($ispost){
            $id = $_POST["viewId"];
            $view = $model->getAll()->where("id",$id)->select();
            return json([
                'code' => 200,
                'view'=>$view,
            ]);
        }


        $this->assign('currentPage',$result->currentPage());
        $this->assign('lastPage',$result->lastPage());
        $this->assign('page',$page);
        $this->assign('account',$account);
        $this->assign('device',$device);
        $this->assign('order',$result);
        $this->assign('count',$count);
        $this->assign('empty',"暂无数据");
        return $this->fetch();
    }

    //新增数据
    public function create(){
        $model = new WorkOrderModel;
        //接收ajax传递的值
        $ispost = $this->request->isPost();
        if($ispost){
            $formdata = $this->request->post();
            $username = Db::query("select id,title from ". $this->dbTablePrefix ."order where `title`='".$formdata['title']."' and deleted_at is NULL ");

            if(!empty($username)){
                return json([
                    'code' => 4000,
                    'message'=>"用户名已存在"
                ]);
            }
            $result = $model->allowField(true)->save($formdata);

            if($result == 1){
                return json([
                    'code' => 200,
                ]);
            }

        }
    }

    //修改数据处理
    public function edit($id)
    {
        $model = new WorkOrderModel;
        //接收ajax传递的值
        $ispost = $this->request->isPost();

        if($ispost){
            $formdata = $this->request->post();

            $result = $model->allowField(true)->save($formdata,['id'=>$id]);

            if($result == 1){
                return json([
                    'code' => 200,
                ]);
            }

        }
    }

    //删除单条记录
    public function delete($id)
    {
        $result = WorkOrderModel::update(['deleted_at'=>date('Y-m-d H:i:s')],['id'=>$id]);//软删除

        if($result){
            return json([
                'code' => 200,
            ]);
        }

    }

    //批量删除
    public function deleteArr()
    {
        $ispost = $this->request->isPost();
        if ($ispost) {
            $idArr = $this->request->post();

            $result = WorkOrderModel::where('id','exp','in('.implode(',',$idArr['id']).')')->update(['deleted_at'=>date('Y-m-d H:i:s')]);//软删除

            if($result){
                return json([
                    'code' => 200,
                ]);
            }

        }
    }
}